package com.yifei.model.result;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yifei.model.enums.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 分页响应结构体
 *
 * @author haoxr
 * @since 2022/2/18 23:29
 */
@Data
@ApiModel(description = "分页统一响应类")
public class PageResult<T> implements Serializable {
    /**
     * 响应状态码
     */
    @ApiModelProperty(value = "响应状态码")
    private String code;

    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    private PageVo<T> data;

    /**
     * 响应消息
     */
    @ApiModelProperty(value = "响应消息")
    private String msg;

    public static <T> PageResult<T> success(IPage<T> page) {
        PageResult<T> result = new PageResult<>();
        // 分页都是成功
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        // 写入数据集合，总数据条数
        result.setData(new PageVo<>(page.getRecords(), page.getTotal()));
        return result;
    }

    @Data
    @Builder
    @AllArgsConstructor
    @ApiModel("分页展示类")
    public static class PageVo<T> {

        @ApiModelProperty(value = "数据集合")
        private List<T> list;

        @ApiModelProperty(value = "总数据条数")
        private long total;
    }
}
